package com.ybxy;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

public class MapDemo {
    public static void main(String[] args) {
       //test0();
        //test1();
        test3();
    }

    private static void test0() {
        Map<String, Double> map = new HashMap<>();
        map.put("张三", 90.0);
        map.put("李四", 80.0);
        map.put("王五", 70.0);
        map.put("赵六", 60.0);
        map.put("张三",100.0);


        System.out.println(map);
        map.remove("张三");
        System.out.println(map);

        boolean b = map.containsKey("李四");
        System.out.println(b);

        boolean b1 = map.containsValue(80.0);
        System.out.println(b1);

        boolean empty = map.isEmpty();
        System.out.println("empty="+empty);

        int size = map.size();
        System.out.println("size="+size);
    }

    private static void test1() {
        Map<String, Double> map1 = new HashMap<>();
        map1.put("张三", 90.0);
        map1.put("李四", 80.0);
        map1.put("王五", 70.0);
        map1.put("赵六", 60.0);

        Set<String> keySet = map1.keySet();

        for (String key : keySet) {
            Double value = map1.get(key);
            System.out.println(key+"="+value);
        }

    }

    public static void  test2() {
        Map<String, Double> map1 = new HashMap<>();
        map1.put("张三", 90.0);
        map1.put("李四", 80.0);
        map1.put("王五", 70.0);
        map1.put("赵六", 60.0);

        //将map中的集合转换成set集合

        Set<Map.Entry<String, Double>> entrySet = map1.entrySet();

       for (Map.Entry<String,Double>entry:entrySet){
           String key = entry.getKey();
           Double value = entry.getValue();
           System.out.println(key+"="+value);
       }
    }


    //Lambda表达式遍历hashmap
    public static void test3() {
        Map<String, Double> map1 = new HashMap<>();
        map1.put("张三", 90.0);
        map1.put("李四", 80.0);
        map1.put("王五", 70.0);
        map1.put("赵六", 60.0);

        map1.forEach(new BiConsumer<String, Double>() {
            @Override
            public void accept(String s, Double aDouble) {
                System.out.println(s+"="+aDouble);
            }
        });
        System.out.println("------------------------------------");
        map1.forEach((key,value)->System.out.println(key+"="+value));
    }
}
